﻿using System;
using System.Collections.Generic;
using Mono.Cecil.Metadata;
using Mono.Collections.Generic;

namespace Mono.Cecil
{
	// Token: 0x020000D6 RID: 214
	internal sealed class TypeDefinitionCollection : Collection<TypeDefinition>
	{
		// Token: 0x06000898 RID: 2200 RVA: 0x00005334 File Offset: 0x00003534
		internal TypeDefinitionCollection(ModuleDefinition container)
		{
			this.container = container;
			this.name_cache = new Dictionary<Row<string, string>, TypeDefinition>(new RowEqualityComparer());
		}

		// Token: 0x06000899 RID: 2201 RVA: 0x00005355 File Offset: 0x00003555
		internal TypeDefinitionCollection(ModuleDefinition container, int capacity) : base(capacity)
		{
			this.container = container;
			this.name_cache = new Dictionary<Row<string, string>, TypeDefinition>(capacity, new RowEqualityComparer());
		}

		// Token: 0x0600089A RID: 2202 RVA: 0x00005378 File Offset: 0x00003578
		protected override void OnAdd(TypeDefinition item, int index)
		{
			this.Attach(item);
		}

		// Token: 0x0600089B RID: 2203 RVA: 0x00005378 File Offset: 0x00003578
		protected override void OnSet(TypeDefinition item, int index)
		{
			this.Attach(item);
		}

		// Token: 0x0600089C RID: 2204 RVA: 0x00005378 File Offset: 0x00003578
		protected override void OnInsert(TypeDefinition item, int index)
		{
			this.Attach(item);
		}

		// Token: 0x0600089D RID: 2205 RVA: 0x00005383 File Offset: 0x00003583
		protected override void OnRemove(TypeDefinition item, int index)
		{
			this.Detach(item);
		}

		// Token: 0x0600089E RID: 2206 RVA: 0x00025158 File Offset: 0x00023358
		protected override void OnClear()
		{
			foreach (TypeDefinition type in this)
			{
				this.Detach(type);
			}
		}

		// Token: 0x0600089F RID: 2207 RVA: 0x000251AC File Offset: 0x000233AC
		private void Attach(TypeDefinition type)
		{
			bool flag = type.Module != null && type.Module != this.container;
			if (flag)
			{
				throw new ArgumentException("Type already attached");
			}
			type.module = this.container;
			type.scope = this.container;
			this.name_cache[new Row<string, string>(type.Namespace, type.Name)] = type;
		}

		// Token: 0x060008A0 RID: 2208 RVA: 0x0000538E File Offset: 0x0000358E
		private void Detach(TypeDefinition type)
		{
			type.module = null;
			type.scope = null;
			this.name_cache.Remove(new Row<string, string>(type.Namespace, type.Name));
		}

		// Token: 0x060008A1 RID: 2209 RVA: 0x0002521C File Offset: 0x0002341C
		public TypeDefinition GetType(string fullname)
		{
			string @namespace;
			string name;
			TypeParser.SplitFullName(fullname, out @namespace, out name);
			return this.GetType(@namespace, name);
		}

		// Token: 0x060008A2 RID: 2210 RVA: 0x00025244 File Offset: 0x00023444
		public TypeDefinition GetType(string @namespace, string name)
		{
			TypeDefinition typeDefinition;
			bool flag = this.name_cache.TryGetValue(new Row<string, string>(@namespace, name), out typeDefinition);
			TypeDefinition result;
			if (flag)
			{
				result = typeDefinition;
			}
			else
			{
				result = null;
			}
			return result;
		}

		// Token: 0x04000341 RID: 833
		private readonly ModuleDefinition container;

		// Token: 0x04000342 RID: 834
		private readonly Dictionary<Row<string, string>, TypeDefinition> name_cache;
	}
}
